package com.imau.common;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class R<T> {

    private Integer code; // 状态码

    private String message; // 提示信息

    private T data; //数据

    private Map map = new HashMap(); // 动态数据


    public static <T> R<T> success(T object, ResultCodeEnum resultCodeEnum) {
        R<T> r = new R<T>();
        r.data = object;
        r.message = resultCodeEnum.getMessage();
        r.code = resultCodeEnum.getCode();
        return r;
    }

    public static <T> R<T> success(ResultCodeEnum resultCodeEnum) {
        R<T> r = new R<T>();
        r.message = resultCodeEnum.getMessage();
        r.code = resultCodeEnum.getCode();
        return r;
    }

    public static <T> R<T> success(ResultCodeEnum resultCodeEnum, String msg) {
        R<T> r = new R<T>();
        r.message = msg;
        r.code = resultCodeEnum.getCode();
        return r;
    }

    public static <T> R<T> error(ResultCodeEnum resultCodeEnum) {
        R r = new R();
        r.message = resultCodeEnum.getMessage();
        r.code = resultCodeEnum.getCode();
        return r;
    }

    public static <T> R<T> error(ResultCodeEnum resultCodeEnum, String msg) {
        R r = new R();
        r.message = msg;
        r.code = resultCodeEnum.getCode();
        return r;
    }

    public R<T> add(String key, Object value) {
        this.map.put(key, value);
        return this;
    }

}
